package com.laborder.sysyy.controller.user;


import com.laborder.sysyy.common.Constant;
import com.laborder.sysyy.entity.User;
import com.laborder.sysyy.service.dictionary.DictionaryService;
import com.laborder.sysyy.service.log.LogService;
import com.laborder.sysyy.service.user.UserService;
import com.laborder.sysyy.utils.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Function: 用户控制层
 * @author: 786107529@qq.com FJL
 * @date:2018/8/25 11:27
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private LogService logService;

	@GetMapping("toUser")
	public String toUser(){
		return "user/userList";
	}

	/**
	 * @Function: 分页查询
	 * @author: 786107529@qq.com FJL
	 * @date:2018/8/26 11:18
	 */
	@ResponseBody
	@RequestMapping(value="findUserWithPage")
	public Map<String,Object> findUserWithPage(int page, int limit, String userName,String realName){
		Map<String, Object>map =new HashMap<String,Object>();
		map.put("page", (page-1)*limit);
		map.put("limit", limit);
		map.put("userName", userName);
		map.put("realName", realName);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("code", 0);
		resultMap.put("msg", "");
		resultMap.put("count", userService.getUserCount(map));
		resultMap.put("data", userService.findUserWithPage(map));
		return resultMap;
	}
	@GetMapping("getAddUser")
	public String getAddUser(Model model){


		return "user/addUser";
	}
	/**
	 * @Function: 用户添加
	 * @author: 786107529@qq.com FJL
	 * @date:2018/8/26 11:40
	 */
	@ResponseBody
	@RequestMapping(value="saveUserInfo")
	public boolean saveUserInfo(User user, HttpServletRequest request) {
		boolean flag = false;
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user1 = (User)session.getAttribute("user");
		try{
			user.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			user.setPassword("123456");
			user.setIsOne(user.getDeptId());
			user.setCreateBy(user1.getUserName());
			List<User> userList = userService.findAll();
			for(User u :userList){
				if(u.getUserName().equals(user.getUserName())){
					return false;
				}
			}
		}catch (Exception e){
			flag = false;
		}
		flag = userService.saveUserInfo(user);
		logService.save(user1.getUserName(),Constant.USER_ADD+"--添加用户："+user.getUserName(),request);
		return flag;
	}

	/**
	 * @Function: 信息回显
	 * @author: 786107529@qq.com FJL
	 * @date:2018/8/26 15:13
	 */

	@RequestMapping("editUser")
	public String planningUpdate(Model model, int userId){

		User user = userService.getUserInfoById(userId);
		model.addAttribute("user",user);

		return "user/editUser";
	}
/**
 * @Function: 更新操作
 * @author: 786107529@qq.com FJL
 * @date:2018/8/26 15:11
 */
	@ResponseBody
	@RequestMapping("updateUserInfo")
	public boolean updateProjectInfo(User user, HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user1 = (User) session.getAttribute("user");
//		user.setIsOne(user.getDeptId());
		logService.save(user.getUserName(),Constant.USER_EDIT+"--被修改用户："+user1.getUserName(),request);
		user.setLastUpdateDate(DateUtils.getDateTime());
		user.setLastUpdateBy(user.getUserName());
		return userService.updateUserInfo(user);

	}
	/**
	 * @Function: 删除操作
	 * @author: 786107529@qq.com FJL
	 * @date:2018/8/26 15:38
	 */
	@ResponseBody
	@RequestMapping("delUserInfo")
	public int delUserInfo(int userId, HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user1 = (User) session.getAttribute("user");
		int flag = 0;
		User user = userService.getUserInfoById(userId);
		if("admin".equals(user.getUserName())){
			flag = -1;
		}else{
			try{
				flag = userService.delUserInfo(userId);
				logService.save(user1.getUserName(),Constant.USER_DELETE+"--被删除用户："+user.getUserName(),request);
			}catch (Exception e){
				flag = 0;
			}

		}
		 return flag;
	}
	/**
	 * @Function: 修改密码
	 * @author: 786107529@qq.com FJL
	 * @date:2018/8/26 20:01
	 */
	@ResponseBody
	@RequestMapping("editPassword")

	public boolean editPassword(User user, HttpServletRequest request){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user2 = (User) session.getAttribute("user");
		boolean flag = false;
		User user1 = userService.getUserInfoById(user.getUserId());
		if(!user1.getPassword().equals(user.getPassword())){
			flag = false;
		}else{
			user.setPassword(user.getNewPassword());
			flag = userService.editPassword(user);
			logService.save(user2.getUserName(),Constant.USER_EDITPASS+"-修改用户："+user.getUserName(),request);
		}
		return flag;
	}
	/**
	 * @Function: 解除权限
	 * @author: 786107529@qq.com FJL
	 * @date:2018/9/16 16:15
	 */
	@ResponseBody
	@RequestMapping("delUserRole")
	public boolean delUserRole(User user, HttpServletRequest request){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user1 = (User) session.getAttribute("user");
		logService.save(user1.getUserName(),Constant.USER_REMOVEROLE+"---被解除用户："+user.getUserName(),request);
		return userService.delUserRole(user);
	}

}
